import {POPULAR_REFRESH,
  POPULAR_REFRESH_SUCCESS,
  POPULAR_REFRESH_FAIL,
  POPULAR_LOAD_MORE_SUCCESS,
  POPULAR_LOAD_MORE_FAIL} from '../types'

let initState = {};
/**
 * // 获取最热页面的异步数据
 * popular:{
 *   java:{
 *     items:[],
 *     isLoading: true
 *   },
 *   ios:{
 *     items:[],
 *     isLoading: false
 *   }
 * }
 * @param preState
 * @param action
 * @returns {*}
 */
export default function (preState=initState,action) {
  switch(action.type){
    case POPULAR_REFRESH:   // 刷新
      return {
        ...preState,
        [action.storeName]:{
          ...preState[action.storeName],
          isLoading: true,   // 正在加载
          hideLoadingMore: true,   // 隐藏加载更多
        }
      };
    case POPULAR_REFRESH_SUCCESS:  // 下拉刷新成功
      return {
        ...preState,
        [action.storeName]:{
          ...preState[action.storeName],
          items: action.items,  // 原始数据
          projectModes:action.projectModes,  //此次要展示的数据
          isLoading: false,
          hideLoadingMore: false,
          pageIndex: action.pageIndex,
        }
      };
    case POPULAR_REFRESH_FAIL:  // 下拉刷新失败
      return {
        ...preState,
        [action.storeName]:{
          ...preState[action.storeName],
          isLoading: false
        }
      };
    case POPULAR_LOAD_MORE_SUCCESS:  //上拉加载更多成功
      return {
        ...preState,
        [action.storeName]:{
          ...preState[action.storeName],
          projectModes: action.projectModes,
          hideLoadingMore: false,
          pageIndex:action.pageIndex,
        }
      };
    case POPULAR_LOAD_MORE_FAIL: //上拉加载更多失败
      console.log('POPULAR_LOAD_MORE_FAIL');
      return {
        ...preState,
        [action.storeName]:{
          ...preState[action.storeName],
          hideLoadingMore: true,
          pageIndex:action.pageIndex,
        }
      };
    default:
      return preState
  }
}